function arr2tree(arr) {
  const resultArr = []

  arr.forEach((item) => {
    if (item.pid === 0) {
      resultArr.push(item)
    } else {
      const targetParent = arr.find((arrItem) => arrItem.id == item.pid)
      if (targetParent) {
        targetParent.children.push(item)
      }
    }
  })

  return resultArr
}

export { arr2tree }
